var add_Data = function (pageSize, pageNumber) {
    var loadDiv = load("#body", 9999999);
    isNulltoWhat(pageSize, 8);
    isNulltoWhat(pageNumber, 1);
    $(".list-content").children("table").children("tbody").html("");
    $(".chooseNum").children("select").html("");
    $.ajax({
        url: "repair/myTask",
        type: "post",
        data: {
            "token": localStorage['token'],
            "pageSize": pageSize,
            "pageNumber": pageNumber

        },
        success: function (data) {
            console.log(data);
            if (data.code == 200) {
                listSetting(data.data.totalPages, data.data.pageNumber);
                localStorage['pageSize'] = data.data.pageSize + "";
                localStorage['pageNumber'] = data.data.pageNumber + "";
                var length = data.data.content.length;
                for (var j = 0; j < data.data.totalPages; j++) {
                    var option = addRealOption(j + 1);
                    if ((j + 1) == data.data.pageNumber) {
                        option.attr("selected", "selected");
                    }
                    option.appendTo($(".chooseNum").children("select"));
                }
                for (var i = 0; i < length; i++) {
                    var task = data.data.content[i];
                    var tr = $('<tr></tr>');
                    var state = stateChangeText(task.state);
                    addTd(task.device).appendTo(tr);
                    addTd(task.content).appendTo(tr);
                    addTd(task.dorm).appendTo(tr);
                    // addTd(task.creator).appendTo(tr);
                    addTd(state).appendTo(tr);
                    addTd(formatTime(task.updateTime)).appendTo(tr);
                    if (task.state == 2) {
                        $("<button onclick='deal($(this))')>处理完毕</button>").appendTo(tr);
                    } else if (task.state == 3) {
                        $("<button class='button_cannot' disabled='disabled' >已处理</button>").appendTo(tr);
                    }
                    addTdHidId(task.id).appendTo(tr);
                    tr.appendTo($(".list-content").children("table").children("tbody"))
                }
                $(".thisNum").children("span").text(data.data.pageNumber);
            } else {
                errorMessage(data.msg);
            }

            load_close(loadDiv);
        }
    });
};

var deal = function (btn) {
    swal(
        {
            title: "提示",
            text: "确定已经完成维修吗?",
            type: "warning",
            buttons: ["取消", "确定！"],
            dangerMode: true
        }
    ).then((willDelete) => {
        if (willDelete) {
            var id = btn.next().text();
            // var loadDiv = load("#body", 9999999);
            // console.log(id);
            $.ajax({
                url: "repairTask/deal",
                type: "post",
                data: {
                    "token": localStorage['token'],
                    "id": id
                },
                success: function (data) {
                    console.log(data);
                    if (data.code == 200) {
                        add_Data(localStorage['pageSize'], localStorage['pageNumber']);
                        // }, 200
                        // );
                    } else {
                        errorMessage(data.msg);
                    }
                }
            })
        } else {

        }
    })

}
var stateChangeText = function (code) {
    var msg = "";
    if (code == 0) {
        msg = "审核中";
    } else if (code == 1) {
        msg = "分配任务中";
    } else if (code == 2) {
        msg = "处理中";
    } else if (code == 3) {
        msg = "已处理";
    } else if (code == 4) {
        msg = "未通过审核";
    }
    return msg;
};


var repair_detail = function (btn) {
    var id = btn.parent().next().text();
    var stateCode = btn.parent().next().next().text();
    localStorage['state'] = stateCode;
    console.log(id);
    var url = "/page/student/repairDetail";
    $("#body").load(url);
};


$("#next").click(function () {
    if (!$(this).hasClass("notAllow")) {
        var pageNumber = parseInt($(".thisNum").children("span").text()) + 1;
        // load($(".list"), 1000);
        add_Data(localStorage['pageSize'], pageNumber);
    }

});

$("#last").click(function () {
    if (!$(this).hasClass("notAllow")) {
        var pageNumber = parseInt($(".thisNum").children("span").text()) - 1;
        add_Data(localStorage['pageSize'], pageNumber);
    }

});


$(".chooseNum").children("select").change(function () {

    add_Data(localStorage['pageSize'], $(this).val());
});
